﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using myBoard.Tools.NativeAPI;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace myBoard.Tools
{
    public class Screen
    {
        /// <summary>
        /// Capture l'écran
        /// </summary>
        /// <param name="path">Path où sera sauvegardée l'image</param>
        public void Capture(string path)
        {
            int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()),
hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,
GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10)); GDI32.SelectObject(hdcDest, hBitmap);
            GDI32.BitBlt(hdcDest, 0, 0, GDI32.GetDeviceCaps(hdcSrc, 8),
            GDI32.GetDeviceCaps(hdcSrc, 10), hdcSrc, 0, 0, 0x00CC0020);
            SaveImageAs(hBitmap, path, ImageFormat.Jpeg);
            Cleanup(hBitmap, hdcSrc, hdcDest);
        }

        public System.Windows.Media.Imaging.BitmapImage Capture()
        {
            int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()),
hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,
GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10)); GDI32.SelectObject(hdcDest, hBitmap);
            GDI32.BitBlt(hdcDest, 0, 0, GDI32.GetDeviceCaps(hdcSrc, 8),
            GDI32.GetDeviceCaps(hdcSrc, 10), hdcSrc, 0, 0, 0x00CC0020);
            Bitmap image =
            new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
            Image.FromHbitmap(new IntPtr(hBitmap)).Width,
            Image.FromHbitmap(new IntPtr(hBitmap)).Height);
            Cleanup(hBitmap, hdcSrc, hdcDest);
            return BitmapOperations.BitmapToBitmapImage(image);
        }

        public Bitmap CaptureDesktopArea(int width, int height, int top, int left, string path)
        {
            Bitmap bmpScreenshot;
            Graphics gfxScreenshot;
            // Set the bitmap object to the size of the screen
            bmpScreenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            // Create a graphics object from the bitmap
            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            // Take the screenshot from the upper left corner to the right bottom corner
            gfxScreenshot.CopyFromScreen(left, top, 0, 0, new System.Drawing.Size(width, height), CopyPixelOperation.SourceCopy);

            // Save the screenshot to the specified path that the user has chosen
            
            //bmpScreenshot.Save(path, ImageFormat.Png);
            return bmpScreenshot;

        }

        private void Cleanup(int hBitmap, int hdcSrc, int hdcDest)
        {
            User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc);
            GDI32.DeleteDC(hdcDest);
            GDI32.DeleteObject(hBitmap);
        }
        private void SaveImageAs(int hBitmap, string fileName, ImageFormat imageFormat)
        {
            Bitmap image =
            new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
            Image.FromHbitmap(new IntPtr(hBitmap)).Width,
            Image.FromHbitmap(new IntPtr(hBitmap)).Height);
            FileInfo fi = new FileInfo(fileName);
            try
            {
                image.Save(fileName, imageFormat);
            }
            catch
            { }
        }
    }
}
